<?php

namespace App\Admin\Controllers;

use App\Models\Ad;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class AdController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('广告');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('广告');
            $content->description('修改');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('广告');
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Ad::class, function (Grid $grid) {

            $grid->model()->orderBy('weight', 'DESC');
            $grid->weight('权重')->sortable();
            $grid->id('ID')->sortable();
            $grid->name('名称');
            $grid->pic('图片')->image();
            $grid->status('状态')->display(function ($status) {
                if ($status == 0) {
                    return "<span class=\"label label-primary\">未上线</span>";
                } elseif ($status == 1) {
                    return "<span class=\"label label-success\">上线中</span>";
                } elseif ($status == 2) {
                    return "<span class=\"label label-danger\">已下线</span>";
                }
            });
            $grid->bind_type('绑定类型')->display(function ($bind_type) {
                if ($bind_type == 1) {
                    return "<span class=\"label label-primary\">商品</span>";
                } elseif ($bind_type == 2) {
                    return "<span class=\"label label-warning\">文章</span>";
                } else {
                    return "";
                }
            });
            $grid->bind_id('绑定id')->display(function ($bind_id) {
                return $bind_id ? : '';
            });
            $grid->start_time('上线时间')->display(function ($time) {
                return $time ? : '';
            });
            $grid->end_time('下线时间')->display(function ($time) {
                return $time ? : '';
            });;
            $grid->created_at('创建时间');

            $grid->disableExport();

            $grid->filter(function ($filter) {
                $filter->like('name', '名称');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Ad::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->hidden('id');
            $form->number('weight', '权重');

            $form->text('name', '名称');
            $form->image('pic', '图片')->uniqueName();
            $form->select('status', '状态')->options(['0' => '未上线', '1' => '上线中', '2' => '已下线']);
            $form->select('bind_type', '绑定类型')->options(['0' => '不绑定', '1' => '商品', '2' => '文章']);
            $form->number('bind_id', '绑定id');
            $form->datetime('start_time', '上线时间')->format('YYYY-MM-DD HH:mm:ss')->rules('required|date');
            $form->datetime('end_time', '下线时间')->format('YYYY-MM-DD HH:mm:ss')->rules('required|date');
            $form->saving(function (Form $form) {
                $where = [['name', '=', $form->name]];
                $id = $form->id;
                if (!empty($id)) {
                    $where[] = ['id', '<>', $id];
                }
                $is_exist = Ad::where($where)->first();
                if (!empty($is_exist)) {
                    $error = new MessageBag([
                        'title'   => '错误',
                        'message' => '广告名称已存在',
                    ]);

                    return back()->with(compact('error'));
                }
                $form->start_time = strtotime($form->start_time);
                $form->end_time = strtotime($form->end_time);
            });
        });
    }
}
